/*
 * ImagenHelper.java
 *
 * Created on 26 de septiembre de 2006, 09:41 AM
 *
 * To change this template, choose Tools | Options and locate the template under
 * the Source Creation and Management node. Right-click the template and choose
 * Open. You can then make changes to the template in the Source Editor.
 */

package com.src.computec.radar.imagen.helper;
import com.src.computec.radar.imagen.dto.ImagenDto;
import com.src.computec.radar.imagen.exception.ImagenException;
import com.src.computec.radar.imagen.util.FileDto;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import static com.src.computec.radar.imagen.base.constantes.ConstantesGenerales.PATH_FISICO_COPIA_IMAGEN;
import static com.src.computec.radar.imagen.base.constantes.ConstantesGenerales.NUMERO_MAXIMO_IMAGENES;
import static com.src.computec.radar.imagen.base.constantes.ConstantesGenerales.NUMERO_IMAGENES_ELIMINAR;

/**
 *
 * @author andresoc
 */
public class ImagenHelper {
    
    /** Creates a new instance of ImagenHelper */
    public ImagenHelper() {
    }

    synchronized public void copiarImagenGuiaDistribucion(ImagenDto dto) throws ImagenException {
        String pathFisicoCopiaImagen = PATH_FISICO_COPIA_IMAGEN;
        try {
            File file = new File(dto.getPathImagenPrivado());
            FileInputStream fileInputStream  = new FileInputStream(file);
            FileOutputStream fileOutputStream = new FileOutputStream(pathFisicoCopiaImagen + dto.getNombreImagenPublico());
            FileChannel fileChannelOrigen = fileInputStream.getChannel();
            FileChannel fileChannelDestino = fileOutputStream.getChannel();
            fileChannelOrigen.transferTo(0, fileChannelOrigen.size(), fileChannelDestino);
            fileInputStream.close();
            fileOutputStream.close();
            fileChannelOrigen.close();
            fileChannelDestino.close();
        }
        catch (FileNotFoundException e) {
            throw new ImagenException("ImagenHelper:copiarImagenGuiaDistribucion: " + e.toString(), e);
        }
        catch (IOException e) {
            throw new ImagenException("ImagenHelper:copiarImagenGuiaDistribucion: " + e.toString(), e);
        }
    }

    public boolean existeImagenGuiaDistribucion(ImagenDto dto) throws ImagenException {
        String pathFisicoCopiaImagen = PATH_FISICO_COPIA_IMAGEN;
        File file = new File(pathFisicoCopiaImagen + dto.getNombreImagenPublico());
        if (file.exists()) {
            return true;
        }
        else {
            return false;
        }
    }

    public List getListNamesFilesByDirectory(ImagenDto dto) throws ImagenException {
        List listNamesFiles = new ArrayList();
        File file = new File(dto.getPathImagenPrivado());
        if (file.exists()) {
            String[] arrayNamesFiles = file.list();
            for (int i = 0; i < arrayNamesFiles.length; i++) {
                FileDto fileDto = new FileDto();
                fileDto.setName(arrayNamesFiles[i]);
                listNamesFiles.add(fileDto);
            }
        }
        return listNamesFiles;
    }

    private List getListNamesFilesSortedByLastModified(String pathFisicoCopiaImagen) throws ImagenException {
        List listNamesFilesSorted = new ArrayList();
        File file = new File(pathFisicoCopiaImagen);
        if (file.exists()) {
            String[] arrayNamesFiles = file.list();
            for (int i = 0; i < arrayNamesFiles.length; i++) {
                file = new File(pathFisicoCopiaImagen + arrayNamesFiles[i]);
                FileDto fileDto = new FileDto();
                fileDto.setName(file.getName());
                fileDto.setLastModified(new Timestamp(file.lastModified()));
                listNamesFilesSorted.add(fileDto);
            }
        }
        Collections.sort(listNamesFilesSorted);
        return listNamesFilesSorted;
    }

   synchronized public void garbageCollectorImages() throws ImagenException {
       String pathFisicoCopiaImagen = PATH_FISICO_COPIA_IMAGEN;
       int numeroMaximoImagenes = NUMERO_MAXIMO_IMAGENES;
       int numeroImagenesEliminar = NUMERO_IMAGENES_ELIMINAR;
       List listNamesFilesSorted = this.getListNamesFilesSortedByLastModified(pathFisicoCopiaImagen);
       if (listNamesFilesSorted.size() >= numeroMaximoImagenes) {
           numeroImagenesEliminar += listNamesFilesSorted.size() - numeroMaximoImagenes;
           Iterator iterator = listNamesFilesSorted.iterator();
           int indice = 0;
           while ((iterator.hasNext()) && (indice++ <= numeroImagenesEliminar)) {
               FileDto fileDto = (FileDto) iterator.next();
               File file = new File(pathFisicoCopiaImagen + fileDto.getName());
               file.delete();
           }
       }
   }
}